package com.freshfresh.activity.home;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.FirstListViewAdapter;
import com.freshfresh.index.IndexProInfoBase;
import com.freshfresh.utils.RequestManager;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;

//首页

public class HomeActivity extends Activity{
	Dialog progressDialog;
	List<Map<String,Object>> listmap;
	List<Map<String,Object>> listmap_banner;
	IndexProInfoBase indexHome;
	private ListView lv_index=null;
	FirstListViewAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_first_page);
		lv_index=(ListView)findViewById(R.id.lv_index);
		initListViewData();
	}
	
	public void initListViewData(){
		if (progressDialog == null) {
			progressDialog = Utils.createLoadingDialog(HomeActivity.this,
					"数据加载中……");
		}
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		String ad_url = UrlConstants.home_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							if(!Utils.parseJsonStr(arg0).containsKey("data")){
								Toast.makeText(HomeActivity.this, "服务器异常", 0).show();
								return;
							}
								
							listmap=(List<Map<String,Object>>)Utils.parseJsonStr(arg0).get("data");
							 listmap.add(0, null);
						     listmap.add(0, null);
							
							
							progressDialog.show();
							Map<String, String> params = new HashMap<String, String>();
							String banner_url = UrlConstants.banner_url;
							Log.e("url是多少…………", banner_url);
							executeRequest(new StringRequest(banner_url, params,
									new Response.Listener<String>() {
										public void onResponse(String arg0) {
											progressDialog.dismiss();
											Log.e("listmap的……", arg0);
											if (Utils.parseJsonStr(arg0).get("result").toString()
													.equals("1")) {
												Map<String,Object> map_banner=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
												listmap_banner=(List<Map<String,Object>>)map_banner.get("banner_list");
												
												adapter =new FirstListViewAdapter(HomeActivity.this, listmap, listmap_banner);
												lv_index.setAdapter(adapter);
												Toast.makeText(HomeActivity.this, listmap.size()+"", 1).show();
												
												
											} else {
												
											}
										}

									}, new Response.ErrorListener() {

										public void onErrorResponse(VolleyError arg0) {
											// dialog.dismiss();
											Toast.makeText(HomeActivity.this, "获取失败", 1).show();
											progressDialog.dismiss();
										}
									}));
							
							
//							adapter=new FirstListViewAdapter(HomeActivity.this, listmap);
//							lv_index.setAdapter(adapter);
//							Toast.makeText(HomeActivity.this, listmap.size()+"", 1).show();
							
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(HomeActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	}
	 protected void executeRequest(Request request) {
	        RequestManager.addRequest(request, this);
	    }
	 
	 

	
}
